﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Commons.DataFilters;
using System.Collections.ObjectModel;
using SD.Commons;
using System.Globalization;

namespace SD.Web.Iteration1.Government
{
    public partial class GovOfficeRegionList : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new GovOfficeRegionFilter();
                ViewState["filter"] = filter;
                ViewState["sortingButton"] = LinkButtonAll.UniqueID;
            }
        }

        /// <summary>
        /// Handles the Selecting event of the ObjectDataSourceGor control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceGovOfficeRegion_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as GovOfficeRegionFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the Command event of the FilterByLetters control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as GovOfficeRegionFilter;
            if (filter != null)
            {
                Collection<string> beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);

                var letterGroupId = ViewState["sortingButton"] as string;
                var letterGroupButton = FindControl(letterGroupId) as LinkButton;
                if (letterGroupButton != null)
                {
                    var clickedButton = sender as LinkButton;
                    if (clickedButton == null)
                    {
                        return;
                    }

                    letterGroupButton.CssClass = string.Empty;
                    clickedButton.CssClass = "hg-blue";
                    ViewState["sortingButton"] = clickedButton.UniqueID;
                }

                GridViewGovOfficeRegion.PageIndex = 0;
                GridViewGovOfficeRegion.DataBind();
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as GovOfficeRegionFilter;
            if (filter != null) filter.IncludeInactive = CheckBoxInactive.Checked;
            GridViewGovOfficeRegion.DataBind();
        }

        /// <summary> 
        /// Handles the Command event of the LinkButtonOrganizationName control. 
        /// </summary> 
        /// <param name="sender">The source of the event.</param> 
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonGORName_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("True"))
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Views/Government/EditGovOfficeRegion.aspx?id={0}", e.CommandName));
            }
            else
            {
                SD.Business.GovOfficeRegionBus.SetActiveStatus(int.Parse(e.CommandName, CultureInfo.CurrentCulture), true);
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Views/Government/EditGovOfficeRegion.aspx?id={0}", e.CommandName));
            }
        }

        /// <summary> 
        /// Handles the RowCreated event of the GridViewOrg control. 
        /// </summary> 
        /// <param name="sender">The source of the event.</param> 
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void GridViewOrg_RowCreated(object sender, GridViewRowEventArgs e)
        {
            var govOfficeRegion = e.Row.DataItem as SD.DataAccess.GovOfficeRegion;
            if (govOfficeRegion != null)
            {
                if (!govOfficeRegion.IsActive)
                {
                    e.Row.Attributes["onclick"] = "return Confirm_Edit();";
                }
            }
        }
    }
}